import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询文章信息
 */
export async function pageArticles(params) {
  const res = await request.get('/admin/cms/article/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加文章信息
 */
export async function addArticle(data) {
  const res = await request.post('/admin/cms/article', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改文章信息
 */
export async function updateArticle(data) {
  const res = await request.put('/admin/cms/article', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除文章信息
 */
export async function removeArticle(id) {
  const res = await request.delete('/admin/cms/article/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除文章信息
 */
export async function removeArticles(ids) {
  const res = await request.delete('/admin/cms/article/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出文章信息
 */
export async function exportArticles(params) {
  const res = await request({
    url: '/admin/cms/article/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `article_${Date.now()}.xlsx`);
}
